package service

import (
	"gitee.com/night-tc/gobige/common"
	"gitee.com/night-tc/gobige/entity"
	"gitee.com/night-tc/gobige/global"
	"gitee.com/night-tc/gobige/msgdef"
	"gitee.com/night-tc/gobige/msgdef/protomsg"
)

// 发生Panic的时候，做的处理
func (this *Service) OnPanic(err error) {

}

// 处理消息前
func (this *Service) OnMsgBefore(name string, msg msgdef.IMsg) {

}

// 处理消息后
func (this *Service) OnMsged(name string, msg msgdef.IMsg) {

}

// 处理RPC消息前
func (this *Service) OnRpcBefore(name string, msg msgdef.IMsg) {

}

// 处理RPC消息后
func (this *Service) OnRpced(name string, msg msgdef.IMsg) {

}

// 处理Srv消息回复
func (this *Service) OnRpcReturn(msg msgdef.IMsg, err error) {

}

// 处理Srv消息回复
func (this *Service) OnSrvReturn(req, msg msgdef.IMsg, err error) {
	if err == nil {
		return
	}
	routerreq := req.(*protomsg.RouteMsgSrvReq)
	if routerreq.BackKey != 0 {
		// 需要回对方服务器
		routerret := new(protomsg.RouteMsgSrvRet)
		routerret.BackKey = routerreq.BackKey
		routerret.RetCode = int32(common.GetErrno(err))
		retmsg := protomsg.NewMsgRetByMsg(msg, err)
		routerret.MsgName = retmsg.Name()
		routerret.MsgData, _ = retmsg.Marshal()
		this.PostSess(routerreq.SourceSrvID, routerret)
	}
	if routerreq.IsRetClient {
		// 需要回客户端
		// 需要eid，如果没有eid做不到回客户端
		if routerreq.EntityID == 0 {
			return
		}
		retmsg := protomsg.NewMsgRetByMsg(msg, err)
		nttutil := entity.NewRedisUtilEntity(routerreq.EntityID)
		srvid, spid, err := nttutil.GetSrvInfoByStype(global.ServerTypeClient)
		if err != nil {
			return
		}
		_ = this.real.PostMsgToClient(srvid, spid, routerreq.EntityID, retmsg)
	}

}
